package thulearningcore;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import thulearningcore.util.TLNullConverter;

public class CourseFileLayer implements Cloneable {
	private String layerID;
	private String layerTitle;

	private boolean updated;

	private ArrayList<CourseFile> courseFileList;

	public CourseFileLayer() {
		this(null, null, null);
	}

	public CourseFileLayer(String layerID, String layerTitle,
			ArrayList<CourseFile> courseFileList) {
		super();
		this.layerID = layerID;
		this.layerTitle = layerTitle;
		this.courseFileList = courseFileList;

		this.updated = false;
	}

	public String getLayerID() {
		return layerID;
	}

	public void setLayerID(String layerID) {
		this.layerID = layerID;
	}

	public String getLayerTitle() {
		return layerTitle;
	}

	public void setLayerTitle(String layerTitle) {
		this.layerTitle = layerTitle;
	}
	
	public void markAllFileAsRead() {
		for(CourseFile c : this.courseFileList) {
			c.markAsRead();
		}
	}

	public ArrayList<CourseFile> getCourseFileList() {
		return courseFileList;
	}

	public CourseFile getCourseFileAt(int i) {
		return courseFileList.get(i);
	}

	public CourseFile getCourseFileWith(int fileID) {
		for (CourseFile cf : this.courseFileList) {
			if (cf.getId() == fileID) {
				return cf;
			}
		}
		return null;
	}

	public void setCourseFileList(ArrayList<CourseFile> courseFileList) {
		this.courseFileList = courseFileList;
	}

	synchronized public boolean loadAllData() {
		return true;
	}

	/**
	 * @return the updated
	 */
	public boolean isUpdated() {
		boolean updated = false;
		for (CourseFile c : this.courseFileList) {
			updated |= c.isUpdated();
		}
		return updated;
		//return updated;
	}

	/**
	 * @param updated
	 *            the updated to set
	 */
	protected void setUpdated(boolean updated) {
		this.updated = updated;
	}

	synchronized protected boolean checkCourseFileListUpdate(
			CourseFileLayer list) {
		boolean updated = false;
		ArrayList<CourseFile> thisList = this.getCourseFileList();
		for (int i = 0; i < thisList.size(); i++) {
			updated |= thisList.get(i).checkUpdate(list.getCourseFileList());
		}
		return this.updated |= updated;
	}

	synchronized protected boolean checkCourseFileListUpdate(
			ArrayList<CourseFileLayer> list) {
		boolean updated = false;
		ArrayList<CourseFile> thisList = this.getCourseFileList();
		for (CourseFile cf : thisList) {
			for (CourseFileLayer cfc : list) {
				boolean find = cf.checkUpdate(cfc.getCourseFileList());
				if (find) {
					updated |= find;
					break;
				}
			}
		}
		return this.updated |= updated;
	}

	public void writeData(DataOutputStream dos) throws IOException {
		dos.writeUTF(TLNullConverter.convert(this.layerID));
		dos.writeUTF(TLNullConverter.convert(this.layerTitle));
		dos.writeBoolean(this.updated);

		if (null != this.courseFileList) {
			dos.writeInt(this.courseFileList.size());
			for (int i = 0; i < courseFileList.size(); i++) {
				courseFileList.get(i).writeData(dos);
			}
		} else {
			dos.writeInt(-1);
		}

	}

	public CourseFileLayer readData(DataInputStream dis) throws IOException {
		this.layerID = TLNullConverter.revert(dis.readUTF());
		this.layerTitle = TLNullConverter.revert(dis.readUTF());
		this.updated = dis.readBoolean();

		int size = dis.readInt();
		if (size != -1) {
			this.courseFileList = new ArrayList<CourseFile>(size);
			for (int i = 0; i < size; i++) {
				courseFileList.add(new CourseFile().readData(dis));
			}
		} else {
			this.courseFileList = null;
		}

		return this;
	}

	public CourseFileLayer clone() throws CloneNotSupportedException {
		CourseFileLayer cfl = (CourseFileLayer) super.clone();
		cfl.courseFileList = null;
		if (null != this.courseFileList) {
			cfl.courseFileList = new ArrayList<CourseFile>(
					this.courseFileList.size());
			for (CourseFile cf : this.courseFileList) {
				cfl.courseFileList.add(cf.clone());
			}
		}
		return cfl;
	}
}
